class ArticlesController < ApplicationController
  before_filter :find_article, :except => [ :index, :new, :create ]
  before_filter :login_required, :except => [ :index, :show ]

  # maps to /articles - named route of home_url
  # HTTP verb: GET 
  def index
    @articles = Article.find(:all, :order => 'created_at DESC', :limit => 10)

    options = { 
      :feed => { :title => "Restolog" },
      :item => { :description => :body }  
    }

    respond_to do |format|
      format.html
      format.xml  { render :xml => @articles.to_xml(:except => [:body]) }
      format.atom { render_atom_feed_for @articles, options } 
      format.rss { render_rss_feed_for @articles, options }
      # format.atom { render :action => "atom", :layout => false, :content_type => Mime::ATOM }
      # format.rss { render :action => "rss", :layout => false, :content_type => Mime::RSS }
    end
  end

  # maps to /articles/new - named route of new_article_url
  def new
  end

  # gets called when for POSTs to /articles
  def create
    @article = Article.new(params[:article])
    @article.user = current_user
    @article.save!
    respond_to do |format|
      format.html do
        flash[:notice] = "Article was successfuly created"
        redirect_to article_url(@article)
      end
      format.xml  do
        headers["Location"] = article_url(@article)
        render :nothing => true, :layout => false, :status => "201 Created"
      end
      # format.js
    end
  rescue
    respond_to do |format|
      format.html { render :action => 'new' }
    end
  end

  # maps to /articles/:id - named route of article_url(article_object)
  # HTTP verb: GET
  def show
    respond_to do |format|
      format.html
      format.xml { render :xml => @article.to_xml(:include => [:comments]) }
   end
  end

  # maps to /articles/:id;edit - named route of edit_article_url(article_object)
  def edit
  end

  # gets called for PUT to /articles/:id
  def update
    @article.attributes = params[:article]
    @article.user = current_user
    @article.save!
    respond_to do |format|
      format.html do
        flash[:notice] = "Article updated"
        redirect_to article_url(@article)
      end
      format.xml  do
        headers["Location"] = article_url(@article)
        render :nothing => true, :layout => false, :status => "201 Created"
      end
      # format.js
    end
  rescue
    respond_to do |format|
      format.html { render :action => 'edit' }
    end
  end

  # maps to /articles/:id;destroy
  # HTTP verb: DELETE
  def destroy
    @article.destroy if request.delete?
    respond_to do |format|
      format.html do
        flash[:notice] = "Article destroyed"
        redirect_to articles_url
      end
      format.xml do
        headers["Location"] = home_url
         render :nothing => true, :layout => false, :status => '204 No Content'
      end
    end
  end

  # maps to /articles/:id;destroy
  # HTTP verb: GET
  # used when JS is disabled
  def delete
  end

  protected
    def find_article
      @article = Article.find(params[:id])
    end
  
    # only article author and admin can delete it
    def authorized?
      return true if ['index', 'new', 'create' ].include?(action_name)
      current_user == @article.user || current_user.login == 'admin'
    end

end
